package com.lixin.smartclassroom.ui.home.tab5.activity

import android.widget.ImageView
import android.widget.TextView
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.R
import com.lixin.smartclassroom.api.getcustomerdetailbycode
import com.lixin.smartclassroom.api.yaoqingclasses
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.common.saveSuccessDialog
import com.lixin.smartclassroom.databinding.ActivityInviteBinding
import com.lixin.smartclassroom.ext.user_name
import com.lixin.smartclassroom.ui.MainActivity
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.getIntentString
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.loadCircle
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.start
import com.lixinkeji.extension.text
import com.lixinkeji.extension.toast
import com.lixinkeji.extension.visible
import per.goweii.anylayer.AnyLayer
import per.goweii.anylayer.ktx.doOnClickToDismiss

/**
 * Description 邀请成员
 * Author: lwc
 * Date:2023/12/8 18:03
 */
class InviteAct : BaseActivity<ActivityInviteBinding>() {
    override fun getViewBinding() = ActivityInviteBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true
    override fun title() = "邀请成员"
    private var classid = ""


    override fun iniView() {
        classid = getIntentString("classid")
        binding.apply {
//            etTeacherName.setText(user_name)
        }
    }


    override fun onClick() {
        binding.btnInvite.onClick {

            scopeNetLife {
                // TODO: 切换查询学生接口
                val baseBean = getcustomerdetailbycode(binding.etHuDongCode.text())
                if (baseBean.result == "0") {
                    if (baseBean.name == binding.etTeacherName.text()) {
                        AnyLayer.dialog(this@InviteAct).contentView(R.layout.dialog_bind_student)
                            .outsideTouchedToDismiss(false)
                            .cancelableOnClickKeyBack(false).backgroundDimAmount(0.5f).bindData {

                                val headImg = it.getView<ImageView>(R.id.headImg)
                                val tvName = it.getView<TextView>(R.id.tvName)
                                val title = it.getView<TextView>(R.id.title)
                                val tvStudentNo = it.getView<TextView>(R.id.tvStudentNo)
                                val tvRole = it.getView<TextView>(R.id.tvRole)
                                val tvInteractionCode = it.getView<TextView>(R.id.tvInteractionCode)
                                val sure = it.getView<TextView>(R.id.sure)

                                sure?.text = "确定"
                                tvName?.text = baseBean.name
                                tvInteractionCode?.text = "互动码：" + baseBean.hudong
                                tvStudentNo?.text = "学号：" + baseBean.xuehao

                                when (baseBean.leixing) {
                                    "1" -> {
                                        tvRole?.text = "学生"
                                        tvStudentNo?.visible()
                                        title?.text = "确定邀请该生？"
                                    }

                                    "2" -> {
                                        tvStudentNo?.gone()
                                        tvRole?.text = "老师"
                                        title?.text = "确定邀请该老师？"
                                    }
                                }
                                headImg?.loadCircle(baseBean.image!!)


                            }.doOnClickToDismiss(R.id.cancel) {

                            }
                            .doOnClickToDismiss(R.id.sure) {
                                scopeNetLife {
                                    val baseBean = yaoqingclasses(
                                        binding.etHuDongCode.text(),
                                        binding.etTeacherName.text(),
                                        classid
                                    )
                                    if (baseBean.result == "0") {
                                        saveSuccessDialog("邀请已发出")
                                    }
                                }
                            }.show()
                    } else {
                        toast("姓名与互动码不一致")
                    }

                } else {
                    toast(baseBean.resultNote.toString())
                }

            }


        }
    }

}